package com.yunxi.core.util;

import java.math.BigDecimal;
import java.math.BigInteger;

/**
 * @author qinxinxin
 * @date 2019/2/19 17:06
 */
public class ConvertUtil {

    //Object转Long
    public static Long objectToLong(Object value){
        if(value == null){
            return 0L;
        }
        Long num = Long.valueOf(String.valueOf(value));
        return num;
    }
    //Object转Double
    public static Double objectToDouble(Object value){
        if(value == null){
            return 0.0;
        }
        Double num = Double.valueOf(String.valueOf(value));
        return num;
    }
    //Object转Integer
    public static Integer objectToIneger(Object value){
        if(value == null){
            return 0;
        }
        Integer num = Integer.valueOf(String.valueOf(value));
        return num;
    }

    //object转bigDecimal
    public static BigDecimal objectToBigDecimal(Object value){
        BigDecimal ret = null;
        if( value != null ) {
            if( value instanceof BigDecimal ) {
                ret = (BigDecimal) value;
            } else if( value instanceof String ) {
                ret = new BigDecimal( (String) value );
            } else if( value instanceof BigInteger) {
                ret = new BigDecimal( (BigInteger) value );
            } else if( value instanceof Number ) {
                ret = new BigDecimal( ((Number)value).doubleValue() );
            } else {
                throw new ClassCastException("Not possible to coerce ["+value+"] from class "+value.getClass()+" into a BigDecimal.");
            }
        }
        return ret;
    }


}
